import 'dart:convert';

import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter_taotaoshipin/app/app_controller.dart';
import 'package:flutter_taotaoshipin/http/request/userinfo_behavior_request.dart';
import 'package:flutter_taotaoshipin/http/request/userinfo_bind_phone_request.dart';
import 'package:flutter_taotaoshipin/http/request/userinfo_center_request.dart';
import 'package:flutter_taotaoshipin/http/request/userinfo_complete_task_request.dart';
import 'package:flutter_taotaoshipin/model/base_entity.dart';
import 'package:flutter_taotaoshipin/model/userinfo_behavior_entity.dart';
import 'package:flutter_taotaoshipin/model/userinfo_bind_phone_entity.dart';
import 'package:flutter_taotaoshipin/model/userinfo_center_entity.dart';
import 'package:flutter_taotaoshipin/model/userinfo_complete_task_entity.dart';
import 'package:get/get.dart';
import 'package:hi_net/core/hi_error.dart';
import 'package:hi_net/hi_net.dart';

/// 用户行为上报类型
enum UserinfoBehaviorEvent {
  tipOff;

  String get value {
    switch (this) {
      case UserinfoBehaviorEvent.tipOff:
        return 'tip_off';
    }
  }
}

/// 举报事件类型
enum UserinfoBehaviorTipOffEvent {
  open,
  close,
  submit;

  int get value {
    switch (this) {
      case UserinfoBehaviorTipOffEvent.open:
        return 1;
      case UserinfoBehaviorTipOffEvent.close:
        return 2;
      case UserinfoBehaviorTipOffEvent.submit:
        return 3;
    }
  }
}

class UserinfoDao {
  /// 初始化接口
  static Future<BaseEntity<UserinfoCenterEntity>> center() async {
    var request = UserinfoCenterRequest();
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserinfoCenterEntity>.fromJson(result);
  }

  // 更新用户中心数据
  static Future<void> updateCenter() async {
    try {
      var result = await UserinfoDao.center();
      if (result.code == 0 && result.data != null) {
        // 请求结果成功，数据不为空
        if (result.data != null) {
          AppController controller = Get.find();
          controller.updateUserinfoCenter(result.data!);
        } else {
          LogUtil.e('数据为空');
        }
      } else {
        LogUtil.d(result.message);
      }
    } on HiNetError catch (e) {
      LogUtil.d(e.message);
    } catch (e) {
      LogUtil.d(e.toString());
    }
  }

  /// 用户绑定手机
  static Future<BaseEntity<UserinfoBindPhoneEntity>> bindPhone({
    required String phone,
    required String code,
  }) async {
    var request = UserinfoBindPhoneRequest();
    request.add('phone', phone);
    request.add('code', code);
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserinfoBindPhoneEntity>.fromJson(result);
  }

  /// 用户任务奖励发放
  static Future<BaseEntity<UserinfoCompleteTaskEntity>> completeTask({
    required String flag,
  }) async {
    var request = UserinfoCompleteTaskRequest();
    request.add('flag', flag);
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserinfoCompleteTaskEntity>.fromJson(result);
  }

  /// 用户行为上报
  static Future<BaseEntity<UserinfoBehaviorEntity>> behavior({
    required String content,
  }) async {
    var request = UserinfoBehaviorRequest();
    request.add('content', content);
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserinfoBehaviorEntity>.fromJson(result);
  }

  /// 用户举报行为上报
  static Future<BaseEntity<UserinfoBehaviorEntity>> trackReport({
    required UserinfoBehaviorTipOffEvent event,
    required int eventType,
  }) async {
    var content = {
      'event': UserinfoBehaviorEvent.tipOff.value,
      'tip_off_event': event.value,
      'tip_off_type': eventType,
    };
    var str = jsonEncode(content);
    var result = await behavior(content: str);
    return result;
  }
}
